package com.ttl.bc.ic.item.service;

import java.util.ArrayList;

import com.ttl.bc.ic.item.BCItemBean;
import com.ttl.bc.ic.item.BCItemDAO;

import com.ttl.bc.master.itemunit.BCItemUnitBean;
import com.ttl.bc.master.itemunit.BCItemUnitDAO;


public class BCItemService {
	
	@SuppressWarnings("unchecked")
	public BCItemShowBean getItem(String id) {

		BCItemShowBean iloc = new BCItemShowBean();
		
		if(id.equals("") || id==null) {
			return iloc;			
		}
		id=id.trim();
		BCItemBean bean = new BCItemBean();				
		BCItemDAO dao = new BCItemDAO();
		ArrayList al = (ArrayList)dao.find(id);
				
		if (al!=null && al.size()>0) {
			bean = (BCItemBean)al.get(0);
			
			bean.setCode(bean.getCode().toUpperCase());
			bean.setName1(bean.getName1().toUpperCase());
			
			iloc.setCode(id.toUpperCase());
			iloc.setItemName(bean.getName1());	
			iloc.setShortName(bean.getShortName());
			iloc.setIttl_row(bean.getIttl_row());
			iloc.setIttl_shelf(bean.getIttl_shelf());
			iloc.setLastBuyPrice(bean.getLastBuyPrice());
			
			System.out.println(iloc.getCode());
			System.out.println(iloc.getItemName());
//			System.out.println(iloc.getIttl_row());
//			System.out.println(iloc.getIttl_shelf());			
//			System.out.println(iloc.getShortName());
//			System.out.println("last buy price="+iloc.getLastBuyPrice());
			
			String hql3 = "FROM BCItemUnitBean p WHERE upper(p.code) =  '"+bean.getDefStkUnitCode()+"'";
			BCItemUnitDAO dao3 = new BCItemUnitDAO();				
			ArrayList al4 = (ArrayList)dao3.find(hql3);			
			
			if (al4!=null && al4.size()>0) {
				BCItemUnitBean bean4 = (BCItemUnitBean)al4.get(0);
				iloc.setUnitCode(bean4.getCode());
				iloc.setUnitName(bean4.getName());								
			}else {
				iloc.setUnitCode("001");
				iloc.setUnitName("Unit");				
			}
		
			//System.out.println(iloc.getUnitCode());
			//System.out.println(iloc.getUnitName());
			
		}else {			
			iloc.setCode("");
			iloc.setItemName("");			
			iloc.setIttl_row("");
			iloc.setIttl_shelf("");			
		}				
								
		return iloc;
	}
}
